<?php
namespace Duobao\Controller;

use Duobao\Model\ShareorderModel as Shareorder;
use Duobao\Model\ShoplistModel as Shoplist;
use Duobao\Controller\LogController as Mylog;

class ShareorderController extends ControllerBase{

    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";
    }

    /**
     * 获取所有的晒单，按时间倒叙
     * 2种情况：第一种传递了商品的id，是获取商品的晒单
     * 第二种：没有传递商品，是所有的晒单
     *
     */
    public function all(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $shareOrder = new Shareorder();

            $offset = I('post.offset');//偏移量
            $length=I('post.limit');//数据长度
            $id=I('post.id');//商品的id

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            if(!empty($id)){
                $item = $shareOrder->all($id,$offset,$length);
            }else{
                $item = $shareOrder->all(0,$offset,$length);
            }

            if ($item) {
                $ret['data']['item'] = $item;
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取晒单，根据id，单品上单内容
     */
    public function getShareOrderById(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $shareOrder = new Shareorder();

            $id = I('post.id');//查询晒单的id

            #判断id是否为空
            if(!empty($id)){
                $item = $shareOrder->getShareOrderById($id);
                if ($item) {
                    $ret['data']['item'] = $item;
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'empty of share id';
            }

        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户晒单操作
     */
    public function setShareOrder(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $model = new Shareorder();

            $uid = $this->getUid(I('get.token'));#用户id
            $goodsId = I('post.goodsId');#商品id
            $title = I('post.title');#用户晒单的标题
            $content = I('post.content');#用户晒单的内容
            $photolist = $_POST['photoList'];#用户晒单图片

            #对用户输入的数据进行判断，判断是否为空
            if(empty($goodsId)){
                E('goods id is empty',403);
            }
            if(empty($title)){
                E('share order title is empty',403);
            }
            if(empty($content)){
                E('share order content is empty',403);
            }
            if(empty($photolist)){
                E('photo list is empty',403);
            }
            if(!is_numeric($uid)){
                E($uid,403);
            }
           # Mylog::write('进入晒单');
            $result = $model->setShareOrder($uid,$goodsId,$title,$content,$photolist);
            #Mylog::write($result);
            if($result){
                $ret['data']['result'] = true;
            }else{
                $ret['status'] = 500;
                $ret['msg'] = '晒单失败';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 根据用户id获取，当前id的用户的所有的晒单列表
     */
    public function getUserShareByUID(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
           $uid = I('post.id');
           $offset = I('post.offset');
           $length = I('post.limit');

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 10;
            }

            if(!empty($uid)){
                $model = new Shareorder();

                $result = $model->getUserShareByUID($uid,$offset,$length);
                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    $ret['status'] = 200;
                    $ret['msg'] = 'the use have not share order';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'request is forbid because the user ID is empty or param type not a int';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 用户心愿单
     * 待完善
     */
    public function getUserWishOrder(){

    }
    /* end of share controller */
}